import React from "react";
import type { DataAction, IProTableRef } from "@yaf/pro-ui";
import { getAgentPageList, getAgentInfo, createAgent, updateAgent, deleteAgent } from "../../api";

export const getDataAction = (proTableRef: React.MutableRefObject<IProTableRef>): DataAction => ({
  fetchList: (params) => {
    const { page, pageSize, filter } = params;
    return getAgentPageList(Object.assign({ page, pageSize }, filter || {})).then((resp) => resp.data)
  },
  getItem: (rowKey) => {
    const id = rowKey as number;
    return getAgentInfo(id).then((resp) => {
      return ({
        name: resp.data.name,
        address: resp.data.address,
        linkman: resp.data.linkman,
        linkmanPhone: resp.data.linkmanPhone,
        status: resp.data.status,
        remark: resp.data.remark,
      });
    })
  },
  createItem: (params) => {
    return createAgent(params).then((resp) => {
      proTableRef.current.doFetchList();
      return resp.data
    })
  },
  updateItem: (rowKey, params) => {
    return updateAgent(rowKey as number, params).then((resp) => {
      proTableRef.current.doFetchList();
      return resp.data
    })
  },
  deleteItems: (selectedRowKeys) => {
    return deleteAgent(selectedRowKeys.map(item => parseInt(item)))
  },
})


